package org.malred.hotelsystem.activities.user;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.BaseActivity;
import org.malred.hotelsystem.activities.admin.ManageUpdateActivity;
import org.malred.hotelsystem.activities.admin.ManagerActivity;
import org.malred.hotelsystem.dao.HotelDao;
import org.malred.hotelsystem.dao.OrderDao;
import org.malred.hotelsystem.model.entity.HotelEntity;
import org.malred.hotelsystem.model.entity.OrderEntity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class UserHotelDetailActivity extends BaseActivity {
    private static final String TAG = "HotelDetailActivity";

    TextView tv_title;
    TextView tv_desc;
    TextView tv_num;

    Button subBtn;

    // 传递来的旅店id
    String hotelId;
    // 传递来的用户id
    String userId;

    @Override
    protected int initLayout() {
        return R.layout.activity_user_hotel_detail;
    }

    @Override
    protected void initView() {
        tv_title = findViewById(R.id.user_detail_title);
        tv_desc = findViewById(R.id.user_detail_desc);
        tv_num = findViewById(R.id.user_detail_num);

        subBtn = findViewById(R.id.user_order_btn);
    }

    @Override
    protected void initData() {
        hotelId = (String) intentExtras.get("hotel_id");
        Log.i(TAG, "initData: " + hotelId);
        HotelEntity hotel = HotelDao.getById(getDb(), hotelId);

        // 改为从share里拿
//        userId = (String) intentExtras.get("u_id");
//        Log.i(TAG, "initData: " + userId);
        SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        userId = sharedPreferences.getString("u_id", "");
        Log.i(TAG, "initData: " + userId);

        tv_title.setText(hotel.getTitle());
        tv_desc.setText(hotel.getDescription());
        tv_num.setText("库存: " + String.valueOf(hotel.getNumber()));
        subBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 不能重复预订同一个房间
                if (OrderDao.getByHidUid(getDb(), hotelId, userId) != null) {
                    showToastSync("已经预订过该房间");
                    return;
                }

                // 添加订单
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setId(UUID.randomUUID().toString());
                orderEntity.setStatus("0");
                orderEntity.setoDate(getSdf().format(new Date()));
                orderEntity.setUid(userId);
                orderEntity.setHid(hotelId);

                boolean success = OrderDao.add(getWritableDb(), orderEntity);
                // 将房间库存减一
                if (hotel.getNumber() - 1 == 0) {
                    navigateTo(UserHomeActivity.class);
                    showToastSync("库存不足");
                    return;
                }
                hotel.setNumber(hotel.getNumber() - 1);
                boolean success2 = HotelDao.upt(getWritableDb(), hotel);

                if (success && success2) {
                    navigateTo(UserHomeActivity.class);
                    showToastSync("预订成功");
                } else {
                    navigateTo(UserHomeActivity.class);
                    showToastSync("预订失败");
                }
            }
        });
    }

}
